home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-portable.exe / quodlibet-3.3.0-portable / data / bin / weakref.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  13KB  |  442 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Weak reference support for Python.
  5.  
  6. This module is an implementation of PEP 205:
  7.  
  8. http://www.python.org/dev/peps/pep-0205/
  9. '''
  10. import UserDict
  11. from _weakref import getweakrefcount, getweakrefs, ref, proxy, CallableProxyType, ProxyType, ReferenceType
  12. from _weakrefset import WeakSet
  13. from exceptions import ReferenceError
  14. ProxyTypes = (ProxyType, CallableProxyType)
  15. __all__ = [
  16.     'ref',
  17.     'proxy',
  18.     'getweakrefcount',
  19.     'getweakrefs',
  20.     'WeakKeyDictionary',
  21.     'ReferenceError',
  22.     'ReferenceType',
  23.     'ProxyType',
  24.     'CallableProxyType',
  25.     'ProxyTypes',
  26.     'WeakValueDictionary',
  27.     'WeakSet']
  28.  
  29. class WeakValueDictionary(UserDict.UserDict):
  30.     '''Mapping class that references values weakly.
  31.  
  32.     Entries in the dictionary will be discarded when no strong
  33.     reference to the value exists anymore
  34.     '''
  35.     
  36.     def __init__(self, *args, **kw):
  37.         
  38.         def remove(wr, selfref = ref(self)):
  39.             self = selfref()
  40.             if self is not None:
  41.                 del self.data[wr.key]
  42.  
  43.         self._remove = remove
  44.         UserDict.UserDict.__init__(self, *args, **kw)
  45.  
  46.     
  47.     def __getitem__(self, key):
  48.         o = self.data[key]()
  49.         if o is None:
  50.             raise KeyError, key
  51.         return o
  52.  
  53.     
  54.     def __contains__(self, key):
  55.         
  56.         try:
  57.             o = self.data[key]()
  58.         except KeyError:
  59.             return False
  60.  
  61.         return o is not None
  62.  
  63.     
  64.     def has_key(self, key):
  65.         
  66.         try:
  67.             o = self.data[key]()
  68.         except KeyError:
  69.             return False
  70.  
  71.         return o is not None
  72.  
  73.     
  74.     def __repr__(self):
  75.         return '<WeakValueDictionary at %s>' % id(self)
  76.  
  77.     
  78.     def __setitem__(self, key, value):
  79.         self.data[key] = KeyedRef(value, self._remove, key)
  80.  
  81.     
  82.     def copy(self):
  83.         new = WeakValueDictionary()
  84.         for key, wr in self.data.items():
  85.             o = wr()
  86.             if o is not None:
  87.                 new[key] = o
  88.                 continue
  89.         return new
  90.  
  91.     __copy__ = copy
  92.     
  93.     def __deepcopy__(self, memo):
  94.         deepcopy = deepcopy
  95.         import copy
  96.         new = self.__class__()
  97.         for key, wr in self.data.items():
  98.             o = wr()
  99.             if o is not None:
  100.                 new[deepcopy(key, memo)] = o
  101.                 continue
  102.         return new
  103.  
  104.     
  105.     def get(self, key, default = None):
  106.         
  107.         try:
  108.             wr = self.data[key]
  109.         except KeyError:
  110.             return default
  111.  
  112.         o = wr()
  113.         if o is None:
  114.             return default
  115.         return None
  116.  
  117.     
  118.     def items(self):
  119.         L = []
  120.         for key, wr in self.data.items():
  121.             o = wr()
  122.             if o is not None:
  123.                 L.append((key, o))
  124.                 continue
  125.         return L
  126.  
  127.     
  128.     def iteritems(self):
  129.         for wr in self.data.itervalues():
  130.             value = wr()
  131.             if value is not None:
  132.                 yield (wr.key, value)
  133.                 continue
  134.  
  135.     
  136.     def iterkeys(self):
  137.         return self.data.iterkeys()
  138.  
  139.     
  140.     def __iter__(self):
  141.         return self.data.iterkeys()
  142.  
  143.     
  144.     def itervaluerefs(self):
  145.         """Return an iterator that yields the weak references to the values.
  146.  
  147.         The references are not guaranteed to be 'live' at the time
  148.         they are used, so the result of calling the references needs
  149.         to be checked before being used.  This can be used to avoid
  150.         creating references that will cause the garbage collector to
  151.         keep the values around longer than needed.
  152.  
  153.         """
  154.         return self.data.itervalues()
  155.  
  156.     
  157.     def itervalues(self):
  158.         for wr in self.data.itervalues():
  159.             obj = wr()
  160.             if obj is not None:
  161.                 yield obj
  162.                 continue
  163.  
  164.     
  165.     def popitem(self):
  166.         while None:
  167.             (key, wr) = self.data.popitem()
  168.             o = wr()
  169.             if o is not None:
  170.                 return (key, o)
  171.             return None
  172.  
  173.     
  174.     def pop(self, key, *args):
  175.         
  176.         try:
  177.             o = self.data.pop(key)()
  178.         except KeyError:
  179.             if args:
  180.                 return args[0]
  181.  
  182.         if o is None:
  183.             raise KeyError, key
  184.         return o
  185.  
  186.     
  187.     def setdefault(self, key, default = None):
  188.         
  189.         try:
  190.             wr = self.data[key]
  191.         except KeyError:
  192.             self.data[key] = KeyedRef(default, self._remove, key)
  193.             return default
  194.  
  195.         return wr()
  196.  
  197.     
  198.     def update(self, dict = None, **kwargs):
  199.         d = self.data
  200.         if dict is not None:
  201.             if not hasattr(dict, 'items'):
  202.                 dict = type({ })(dict)
  203.             for key, o in dict.items():
  204.                 d[key] = KeyedRef(o, self._remove, key)
  205.             
  206.         if len(kwargs):
  207.             self.update(kwargs)
  208.  
  209.     
  210.     def valuerefs(self):
  211.         """Return a list of weak references to the values.
  212.  
  213.         The references are not guaranteed to be 'live' at the time
  214.         they are used, so the result of calling the references needs
  215.         to be checked before being used.  This can be used to avoid
  216.         creating references that will cause the garbage collector to
  217.         keep the values around longer than needed.
  218.  
  219.         """
  220.         return self.data.values()
  221.  
  222.     
  223.     def values(self):
  224.         L = []
  225.         for wr in self.data.values():
  226.             o = wr()
  227.             if o is not None:
  228.                 L.append(o)
  229.                 continue
  230.         return L
  231.  
  232.  
  233.  
  234. class KeyedRef(ref):
  235.     """Specialized reference that includes a key corresponding to the value.
  236.  
  237.     This is used in the WeakValueDictionary to avoid having to create
  238.     a function object for each key stored in the mapping.  A shared
  239.     callback object can use the 'key' attribute of a KeyedRef instead
  240.     of getting a reference to the key from an enclosing scope.
  241.  
  242.     """
  243.     __slots__ = ('key',)
  244.     
  245.     def __new__(type, ob, callback, key):
  246.         self = ref.__new__(type, ob, callback)
  247.         self.key = key
  248.         return self
  249.  
  250.     
  251.     def __init__(self, ob, callback, key):
  252.         super(KeyedRef, self).__init__(ob, callback)
  253.  
  254.  
  255.  
  256. class WeakKeyDictionary(UserDict.UserDict):
  257.     ''' Mapping class that references keys weakly.
  258.  
  259.     Entries in the dictionary will be discarded when there is no
  260.     longer a strong reference to the key. This can be used to
  261.     associate additional data with an object owned by other parts of
  262.     an application without adding attributes to those objects. This
  263.     can be especially useful with objects that override attribute
  264.     accesses.
  265.     '''
  266.     
  267.     def __init__(self, dict = None):
  268.         self.data = { }
  269.         
  270.         def remove(k, selfref = ref(self)):
  271.             self = selfref()
  272.             if self is not None:
  273.                 del self.data[k]
  274.  
  275.         self._remove = remove
  276.         if dict is not None:
  277.             self.update(dict)
  278.  
  279.     
  280.     def __delitem__(self, key):
  281.         del self.data[ref(key)]
  282.  
  283.     
  284.     def __getitem__(self, key):
  285.         return self.data[ref(key)]
  286.  
  287.     
  288.     def __repr__(self):
  289.         return '<WeakKeyDictionary at %s>' % id(self)
  290.  
  291.     
  292.     def __setitem__(self, key, value):
  293.         self.data[ref(key, self._remove)] = value
  294.  
  295.     
  296.     def copy(self):
  297.         new = WeakKeyDictionary()
  298.         for key, value in self.data.items():
  299.             o = key()
  300.             if o is not None:
  301.                 new[o] = value
  302.                 continue
  303.         return new
  304.  
  305.     __copy__ = copy
  306.     
  307.     def __deepcopy__(self, memo):
  308.         deepcopy = deepcopy
  309.         import copy
  310.         new = self.__class__()
  311.         for key, value in self.data.items():
  312.             o = key()
  313.             if o is not None:
  314.                 new[o] = deepcopy(value, memo)
  315.                 continue
  316.         return new
  317.  
  318.     
  319.     def get(self, key, default = None):
  320.         return self.data.get(ref(key), default)
  321.  
  322.     
  323.     def has_key(self, key):
  324.         
  325.         try:
  326.             wr = ref(key)
  327.         except TypeError:
  328.             return 0
  329.  
  330.         return wr in self.data
  331.  
  332.     
  333.     def __contains__(self, key):
  334.         
  335.         try:
  336.             wr = ref(key)
  337.         except TypeError:
  338.             return 0
  339.  
  340.         return wr in self.data
  341.  
  342.     
  343.     def items(self):
  344.         L = []
  345.         for key, value in self.data.items():
  346.             o = key()
  347.             if o is not None:
  348.                 L.append((o, value))
  349.                 continue
  350.         return L
  351.  
  352.     
  353.     def iteritems(self):
  354.         for wr, value in self.data.iteritems():
  355.             key = wr()
  356.             if key is not None:
  357.                 yield (key, value)
  358.                 continue
  359.  
  360.     
  361.     def iterkeyrefs(self):
  362.         """Return an iterator that yields the weak references to the keys.
  363.  
  364.         The references are not guaranteed to be 'live' at the time
  365.         they are used, so the result of calling the references needs
  366.         to be checked before being used.  This can be used to avoid
  367.         creating references that will cause the garbage collector to
  368.         keep the keys around longer than needed.
  369.  
  370.         """
  371.         return self.data.iterkeys()
  372.  
  373.     
  374.     def iterkeys(self):
  375.         for wr in self.data.iterkeys():
  376.             obj = wr()
  377.             if obj is not None:
  378.                 yield obj
  379.                 continue
  380.  
  381.     
  382.     def __iter__(self):
  383.         return self.iterkeys()
  384.  
  385.     
  386.     def itervalues(self):
  387.         return self.data.itervalues()
  388.  
  389.     
  390.     def keyrefs(self):
  391.         """Return a list of weak references to the keys.
  392.  
  393.         The references are not guaranteed to be 'live' at the time
  394.         they are used, so the result of calling the references needs
  395.         to be checked before being used.  This can be used to avoid
  396.         creating references that will cause the garbage collector to
  397.         keep the keys around longer than needed.
  398.  
  399.         """
  400.         return self.data.keys()
  401.  
  402.     
  403.     def keys(self):
  404.         L = []
  405.         for wr in self.data.keys():
  406.             o = wr()
  407.             if o is not None:
  408.                 L.append(o)
  409.                 continue
  410.         return L
  411.  
  412.     
  413.     def popitem(self):
  414.         while None:
  415.             (key, value) = self.data.popitem()
  416.             o = key()
  417.             if o is not None:
  418.                 return (o, value)
  419.             return None
  420.  
  421.     
  422.     def pop(self, key, *args):
  423.         return self.data.pop(ref(key), *args)
  424.  
  425.     
  426.     def setdefault(self, key, default = None):
  427.         return self.data.setdefault(ref(key, self._remove), default)
  428.  
  429.     
  430.     def update(self, dict = None, **kwargs):
  431.         d = self.data
  432.         if dict is not None:
  433.             if not hasattr(dict, 'items'):
  434.                 dict = type({ })(dict)
  435.             for key, value in dict.items():
  436.                 d[ref(key, self._remove)] = value
  437.             
  438.         if len(kwargs):
  439.             self.update(kwargs)
  440.  
  441.  
  442.